<?php

namespace app\admin\model;

use think\Model;

/**
 * Activity
 */
class Activity extends Model
{
    // 表名
    protected $name = 'activity';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    // 追加属性
    protected $append = [
        'label',
    ];

    // 字段类型转换
    protected $type = [
        'start_time' => 'timestamp:Y-m-d H:i:s',
        'end_time'   => 'timestamp:Y-m-d H:i:s',
    ];


    public function getLabelAttr($value, $row): array
    {
        return [
            'label' => \app\admin\model\Label::whereIn('id', $row['label_ids'])->column('label'),
        ];
    }

    public function getLabelIdsAttr($value): array
    {
        if ($value === '' || $value === null) return [];
        if (!is_array($value)) {
            return explode(',', $value);
        }
        return $value;
    }

    public function setLabelIdsAttr($value): string
    {
        return is_array($value) ? implode(',', $value) : $value;
    }
}